<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               :upload-error="loadError"
               @on-load="onLoad">
      <!--      <template slot="menuLeft">-->
      <!--        <el-button type="danger"-->
      <!--                   size="small"-->
      <!--                   icon="el-icon-delete"-->
      <!--                   plain-->
      <!--                   v-if="permission.xzxx_delete"-->
      <!--                   @click="handleDelete">删 除-->
      <!--        </el-button>-->
      <!--      </template>-->
      <template slot-scope="{type,size,row,index}" slot="menu">
        <el-button icon="el-icon-tickets" :size="size" :type="type" @click="replyClick(row)">回复</el-button>
      </template>
    </avue-crud>

    <el-dialog title="回复"
               append-to-body
               :visible.sync="excelBox"
               width="555px">
      <avue-form :option="excelOption" v-model="excelForm" :upload-after="uploadAfter">
      </avue-form>
      <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitrep">确 定</el-button>
          <el-button @click="excelBox = false">取 消</el-button>
        </span>
    </el-dialog>

  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove, addOrEditFeedbackContent} from "@/api/service/xzxx";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],

      excelBox: false,
      excelForm: {isAnswer: '0'},
      selectData: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "回复内容",
            prop: "desc",
            // span: 24,
            // disabled: true,
            type: "textarea",
            span: 24,
            minRows: 6,
            rules: [{
              required: true,
              message: "请输入反馈内容",
              trigger: "blur"
            }]
          },
        ]
      },
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        // menuType: 'menu',
        // menuBtnTitle: '更多操作',
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "学校名称",
            prop: "schoolId",
            type: "tree",
            search: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入学校名称",
              trigger: "blur"
            }],
            dicUrl: "/api/PcPublicService/GetSchoolTreeSelect",
            cascader: ['classId'],
            props: {
              label: "xxName",
              value: "id"
            },
            // editDisabled: true,
          },
          // {
          //   label: "校长",
          //   prop: "jsId",
          //   overHidden:true,
          //   rules: [{
          //     required: true,
          //     message: "请输入校长",
          //     trigger: "blur"
          //   }],
          //   type: "select",
          //   editDisabled:true,
          //   dicUrl: "/api/school-service/js/select",
          //   props: {
          //     label: "jsName",
          //     value: "id"
          //   },
          // },
          {
            label: "学生姓名",
            prop: "studentName",
            editDisabled: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入学生姓名",
              trigger: "blur"
            }],
            // type: "select",
            // dicUrl: "/api/school-service/xs/select",
            // props: {
            //   label: "xsName",
            //   value: "id"
            // },
          },
          {
            label: "年级",
            prop: "gradeName",
            editDisabled: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入学生姓名",
              trigger: "blur"
            }],
            // type: "select",
            // dicUrl: "/api/school-service/xs/select",
            // props: {
            //   label: "xsName",
            //   value: "id"
            // },
          },
          {
            label: "班级",
            prop: "className",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            overHidden: true,
            rules: [{
              required: false,
              message: "请输入班级",
              trigger: "blur"
            }],
          },
          {
            label: "班级",
            prop: "classId",
            // search: true,
            hide: true,
            overHidden: true,
            rules: [{
              required: false,
              message: "请输入班级",
              trigger: "blur"
            }],
            type: "select",
            dicUrl: "/api/ManageAppealMailbox/GetSchoolClassSelect?schoolId={{key}}",
            props: {
              label: "name",
              value: "id"
            },
          },
          {
            label: "诉求类型",
            prop: "type",
            rules: [{
              required: true,
              message: "请选择诉求类型",
              trigger: "blur"
            }],
            type: "select",
            search: true,
            dicData: [
              {
                label: "咨询",
                value: 0
              },
              {
                label: "表扬",
                value: 1
              },
              {
                label: "建议",
                value: 2
              },
              {
                label: "投诉",
                value: 3
              },
            ],
            // dataType:'number',
            // editDisabled:true,
            // addDisabled:true
          },
          {
            label: "诉求事项",
            prop: "title",
            // search:true,
            editDisabled: true,
            overHidden: true,
            rules: [{
              required: false,
              message: "请输入诉求事项",
              trigger: "blur"
            }],
          },
          {
            label: "诉求描述",
            prop: "des",
            // search:true,
            overHidden: true,
            hide: true,
            rules: [{
              required: false,
              message: "请输入诉求事项",
              trigger: "blur"
            }],
          },
          {
            label: "联系方式",
            prop: "phone",
            // search:true,
            editDisabled: true,
            overHidden: true,
            rules: [{
              required: false,
              message: "请输入联系方式",
              trigger: "blur"
            }],
          },
          {
            label: "是否匿名",
            prop: "anonymity",
            // search:true,
            hide: true,
            editDisabled: true,
            overHidden: true,
            rules: [{
              required: false,
              message: "请选择是否匿名",
              trigger: "blur"
            }],
          },
          {
            label: "是否回复",
            prop: "isAnswer",
            hide: true,
            rules: [{
              required: true,
              message: "请输入是否回复",
              trigger: "blur"
            }]
          },
          {
            label: "回复内容",
            prop: "desc",
            type: "textarea",
            span: 24,
            minRows: 6,
            hide: true,
            rules: [{
              required: true,
              message: "请输入回复内容",
              trigger: "blur"
            }]
          },
          // {
          //   label: "信箱主题",
          //   editDisabled:true,
          //   prop: "xxTitle",
          //   overHidden:true,
          //   rules: [{
          //     required: true,
          //     message: "请输入信箱主题",
          //     trigger: "blur"
          //   }]
          // },
          // {
          //   label: "信箱内容",
          //   prop: "xxContent",
          //   type: "textarea",
          //   span: 24,
          //   minRows: 6,
          //   rules: [{
          //     required: true,
          //     message: "请输入信箱内容",
          //     trigger: "blur"
          //   }],
          //   hide: true,
          //   editDisabled:true,
          // },
          // {
          //   label: "发信时间",
          //   prop: "xxTime",
          //   type: "datetime",
          //   editDisabled:true,
          //   valueFormat: "yyyy-MM-dd HH:mm:ss",
          //   overHidden:true,
          //   rules: [{
          //     required: false,
          //     message: "请输入发信时间",
          //     trigger: "blur"
          //   }]
          // },
          {
            label: "图1",
            prop: "pic1",
            rules: [{
              required: false,
              message: "请输入图1",
              trigger: "blur"
            }],
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            fileSize: 520,
            listType: 'picture-img',
            accept: "image/*",
            span: 12,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "图2",
            prop: "pic2",
            rules: [{
              required: false,
              message: "请输入图2",
              trigger: "blur"
            }],
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            fileSize: 520,
            listType: 'picture-img',
            accept: "image/*",
            span: 12,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "图3",
            prop: "pic3",
            rules: [{
              required: false,
              message: "请输入图3",
              trigger: "blur"
            }],
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            fileSize: 520,
            listType: 'picture-img',
            accept: "image/*",
            span: 12,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "图4",
            prop: "pic4",
            rules: [{
              required: false,
              message: "请输入图4",
              trigger: "blur"
            }],
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            fileSize: 520,
            listType: 'picture-img',
            accept: "image/*",
            span: 12,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "图5",
            prop: "pic5",
            rules: [{
              required: false,
              message: "请输入图5",
              trigger: "blur"
            }],
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            fileSize: 520,
            listType: 'picture-img',
            accept: "image/*",
            span: 12,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "图6",
            prop: "pic6",
            rules: [{
              required: false,
              message: "请输入图6",
              trigger: "blur"
            }],
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            fileSize: 520,
            listType: 'picture-img',
            accept: "image/*",
            span: 12,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },
          {
            label: "提交时间",
            prop: "createTime",
            type: "datetime",
            editDisabled:true,
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            overHidden:true,
            rules: [{
              required: false,
              message: "请输入提交时间",
              trigger: "blur"
            }]
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: this.vaildData(this.permission.xzxx_view, false),
        delBtn: false,//this.vaildData(this.permission.xzxx_delete, false),
        editBtn: false,//this.vaildData(this.permission.xzxx_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    // 回复
    replyClick(row) {

      getDetail(row.id).then(res => {
        if (res.data.data.feedbackContent === null) {
          this.excelForm.desc = ''
          this.excelForm.isAnswer = '0';
          this.excelForm.parentId = res.data.data.id;
        } else {
          this.excelForm = res.data.data.feedbackContent
          this.excelForm.parentId = res.data.data.id;
          this.excelForm.isAnswer = '1';
        }
        this.excelBox = true
      });
    },
    // 弹窗确定
    submitrep() {
      let params = {
        id: this.excelForm.isAnswer === '0' ? '0' : this.excelForm.id,
        parentId: this.excelForm.parentId,
        desc: this.excelForm.desc
      }
      addOrEditFeedbackContent(params).then(() => {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        this.excelBox = false
      }, error => {
        window.console.log(error);
      });
    },
    loadError(error) {
      this.$message({
        type: "error",
        message: error
      });
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          res.data.data.anonymity = res.data.data.anonymity ? '是' : '否'
          if (res.data.data.feedbackContent === null) {
            res.data.data.isAnswer = '否'
            res.data.data.desc = ''
          } else {
            res.data.data.isAnswer = '是'
            res.data.data.desc = res.data.data.feedbackContent.desc
          }
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      // console.log('11111111',params)
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      console.log(params,this.query)
      // params.type = (this.form.type === '' || this.form.type === undefined) ? '-1' : this.form.type
      this.query.type = (this.query.type === '' || this.query.type === undefined) ? '-1' : this.query.type
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        data.records.forEach(item => {
          item.anonymity = item.anonymity ? '是' : '否'
        })
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
      // const school = this.findObject(this.option.column, "schoolId");
      // school.hide = true;
    },
    uploadAfter(res, done, loading, column) {
      window.console.log(column);
      done();
      this.excelBox = false;
    },
  }
};
</script>

<style>
</style>
